# reference: 20190511_决策树.ipynb
# from sklearn.tree import DecisionTreeClassifier
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.preprocessing import LabelEncoder, OneHotEncoder
from sklearn import tree
import pydotplus 
from graphviz import dot
from IPython.display import Image  

path = "./data/playball.txt"
data_df = pd.read_csv(path, sep=' ')

data_num_df = data_df.copy()
label_Code = LabelEncoder()

for feature in data_num_df.columns:
    data_num_df[feature] = label_Code.fit_transform(data_num_df[feature])

clf = tree.DecisionTreeClassifier(criterion='entropy')
clf.fit(data_num_df[['OutLook', 'Temperature', 'Humidity', 'Wind']], data_num_df.PlayTennis)

dot_data = tree.export_graphviz(clf, out_file=None, 
                         feature_names=['OutLook', 'Temperature', 'Humidity', 'Wind'],  
                         class_names=['No','Yes'],  
                         filled=True, rounded=True,  
                         special_characters=True)  
graph = pydotplus.graph_from_dot_data(dot_data)  
Image(graph.create_png()) 